package packing;
import java.sql.Blob;
import java.sql.SQLException;

/**
 * A storage class that holds a database Blob version of a source file
 * 
 */
public class SourceFile
{
	String name;
	Blob file;
	int kin;

	/**
	 * Constructor to create a new SourceFile
	 * 
	 * @param name
	 *            The name of the file
	 * @param kin
	 *            A number that represent the type of file this is
	 * @param file
	 *            The database blob
	 */
	public SourceFile(String name, int kin, Blob file)
	{
		this.name = name;
		this.file = file;
		this.kin = kin;
	}

	/**
	 * Return the byte array to the Blob content.
	 * 
	 * @return A byte array of the Blob object
	 * @throws SQLException
	 *             Propagate all SQLException to caller
	 */
	public byte[] getByteArray() throws SQLException
	{
		return file.getBytes(1, length());
	}

	/**
	 * Get the length of the file
	 * 
	 * @return File length, return 0 if file is null
	 */
	public int length()
	{
		try
		{
			return (int) file.length();
		} catch (SQLException e)
		{
			return 0;
		}
	}
}
